/********************************
 * @FileName:     MySQLDialect.java
 * @Description:  MySQLDialect.java -
 * @Copyright:    Copyright(C) 2010-2011 mbool.com
 * @Company       MBOOL-TEAM zhangjiayi LTD.
 * @author:       zhangjiayi
 * @version       V1.0 
 * @Createdate:   2016-5-9 下午9:53:20
 *
 * All rights Reserved, Designed By MBOOL-TEAM
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2016-5-9       zhangjiayi       1.0         TODO
 * Why & What is modified: <修改原因描述>
 *
 ********************************/
package com.rowline.admin.module.util.data.dialect;


/**
 * @ClassName:     MySQLDialect
 * @Description:   MySQL的分页方言
 * @author:        zhangjiayi
 * @date:          2016-5-9 下午9:53:20
 *
 */
public class MysqlDialect extends Dialect  {
	
private static final String LIMIT = " LIMIT ";
	
	public boolean supportsLimitOffset() {
		return true;
	}

	public boolean supportsLimit() {
		return true;
	}

	public String getLimitString(String sql, int offset,
			String offsetPlaceholder, int limit, String limitPlaceholder) {
		if (offset > 0) {
			return sql + LIMIT + offsetPlaceholder + "," + limitPlaceholder;
		} else {
			return sql + LIMIT + limitPlaceholder;
		}
	}

	@Override
	public String getLimitString(String sql, int limit) {
		if(!checkLimit(sql)){
			return sql + LIMIT + limit;
		}
		return sql;
	}

	private boolean checkLimit(String sql){
		boolean isLimited = false;
		if(sql.toLowerCase().indexOf(LIMIT)!=-1){
			isLimited = true;
		}
		return isLimited;
	}
}
